package com.icloud.article.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icloud.article.boot.dto.ArticleSensitiveAddDto;
import com.icloud.article.boot.dto.ArticleSensitivePageDto;
import com.icloud.article.boot.dto.ArticleSensitiveUpdateDto;
import com.icloud.article.boot.mapper.ArticleSensitiveMapper;
import com.icloud.article.boot.model.ArticleSensitive;
import com.icloud.article.boot.service.ArticleSensitiveService;
import com.icloud.article.boot.vo.ArticleSensitiveVo;
import com.icloud.common.core.api.ResultCode;
import com.icloud.common.web.exception.Asserts;
import com.icloud.common.web.util.AuthUtil;
import com.icloud.common.web.vo.PageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 动态设置service 实现类
 */
@Service
public class ArticleSensitiveServiceImpl extends ServiceImpl<ArticleSensitiveMapper, ArticleSensitive> implements ArticleSensitiveService {
    @Autowired
    private AuthUtil authUtil;

    @Override
    public PageVO<ArticleSensitiveVo> sensitivePage(ArticleSensitivePageDto dto) {
        PageVO<ArticleSensitiveVo> pageVO = new PageVO<>();
        LambdaQueryWrapper<ArticleSensitive> sensQuery = new LambdaQueryWrapper<>();
        IPage<ArticleSensitive> asPage = baseMapper.selectPage(Page.of(dto.getCurrent(), dto.getPageSize()), sensQuery);
        List<ArticleSensitive> asList = asPage.getRecords();
        if(CollectionUtils.isEmpty(asList)){
            return pageVO;
        }
        List<ArticleSensitiveVo> voList = asList.stream().map(as->{
            ArticleSensitiveVo vo = new ArticleSensitiveVo();
            BeanUtils.copyProperties(as, vo);
            return vo;
        }).collect(Collectors.toList());

        pageVO.setTotal(asPage.getTotal());
        pageVO.setList(voList);
        return pageVO;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean add(ArticleSensitiveAddDto dto) {
        String sw = dto.getSensitiveWords();
        Asserts.fail(StringUtils.isBlank(sw), ResultCode.VALIDATE_FAILED);

        Long loginId = authUtil.getUserId();
        ArticleSensitive as = new ArticleSensitive();
        as.setCreatedId(loginId);
        as.setUpdatedId(loginId);
        as.setSensitiveWords(dto.getSensitiveWords());
        int effect = baseMapper.insert(as);
        return effect>0;
    }

    @Override
    public boolean update(ArticleSensitiveUpdateDto dto) {
        String sw = dto.getSensitiveWords();
        Asserts.fail(StringUtils.isBlank(sw), ResultCode.VALIDATE_FAILED);

        Long loginId = authUtil.getUserId();
        Long id = dto.getId();
        ArticleSensitive as = baseMapper.selectById(id);
        Asserts.fail(null==as, ResultCode.DATA_NOT_EXISTS);

        ArticleSensitive updated = new ArticleSensitive();
        updated.setId(id);
        updated.setSensitiveWords(dto.getSensitiveWords());
        updated.setUpdatedId(loginId);
        int effect = baseMapper.updateById(updated);
        return effect>0;
    }
}
